Skip to content

fix(hmr): hash only emitted bytes in code generation result - #14046

Closed
stormslowly wants to merge 4 commits into
mainfrom
fix/hmr-codegen-result-hash-skip-source-map
Closed

stormslowly wants to merge 4 commits into
mainfrom
fix/hmr-codegen-result-hash-skip-source-map

Conversation

@stormslowly

@stormslowly stormslowly commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Rspack routed through Source::hash, which for SourceMapSource also folds in the wrapped source_map — including its sourcesContent snapshot of the loader-input file.

For files split into multiple sub-modules (Vue SFCs, anything emitting ?type=... virtual modules), this means changing one block shifts the codegen hash of every sibling sub-module even when their emitted bytes are unchanged. The HMR plugin then ships byte-identical modules in hot-update chunks, and anything keyed off "this module changed" misfires.

rspack (before) rspack (after)
Hash inputs full Source::hash (incl. source_map.sourcesContent) + chunk_init_fragments + runtime_requirements source.buffer() + chunk_init_fragments + runtime_requirements
Stable across same-bytes rebuild? yes no

Side effect: fixes #11635

Vue 2 <script setup> parent/child HMR breakage is downstream of this divergence — editing a Vue template made the byte-identical script-setup sub-module ride into hot-update.js, which bubbled into api.reload and broke record.Ctor alignment. With this patch the script-setup stays out of the update and the HMR runtime takes the api.rerender path instead.

Test plan

  • CI green
  • no regression in tests/rspack-test/hotCases/

Local repro: @rsbuild/core@2.0.6 + @rsbuild/plugin-vue2@1.1.1 Vue 2.7 minimal project, Playwright-driven edit sequence [app, app, child, child, app, child]. Before patch: final child template stays on the previous value. After patch: updates correctly, matching webpack 5.106 + vue-loader@15.11.1.

Align `CodeGenerationResult::set_hash` with webpack's
`CodeGenerationResults.getHash`, which feeds `source.updateHash(hash)`
and never mixes source-map metadata into the digest.

In rspack, `Source::hash` for `SourceMapSource` folds in `source_map`
(which carries the `sourcesContent` snapshot of the loader-input file).
For multi-block source files such as Vue SFCs that means changing one
block shifts the codegen hash of every sibling sub-module even when
their emitted bytes are identical. The HMR plugin compares those
hashes to decide which modules go into a hot-update chunk, so it ships
byte-identical modules and downstream HMR flows that key off
"this module changed" (e.g. vue-loader's self-accept on the SFC main
wrapper) misfire.

Switch to `source.buffer()` so the digest only reflects the actually
emitted bytes plus `chunk_init_fragments` and `runtime_requirements`,
matching webpack semantics.
Copilot AI review requested due to automatic review settings May 15, 2026 07:11
@stormslowly
stormslowly marked this pull request as draft May 15, 2026 07:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Aligns CodeGenerationResult::set_hash with webpack's behavior by hashing only the emitted source bytes (via source.buffer()) instead of the full Source::hash, which for SourceMapSource also folded source-map metadata (including sourcesContent) into the digest. This prevents HMR from including byte-identical sibling sub-modules (e.g., from Vue SFCs) in hot-update chunks, fixing the downstream bug reported in #11635.

Changes:

  • Replace source.hash(&mut hasher) with source.buffer().hash(&mut hasher) in CodeGenerationResult::set_hash.
  • Add an explanatory comment describing the rationale and the HMR implications.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d4bda8697a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// shifts the codegen hash of every sibling sub-module even when their
// emitted bytes are identical, which causes HMR to ship unchanged
// modules in hot-update chunks and breaks downstream HMR flows.
source.buffer().hash(&mut hasher);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve inline source-map changes in content hashes

When devtool inlines maps (SourceMapDevToolPlugin's no-filename path appends source_map_json directly into the JS asset), the final emitted .js bytes still change when only the source map changes, e.g. a loader updates sourcesContent while generated module code stays byte-identical. Hashing only source.buffer() here means the JS content hash/version produced from CodeGenerationResult::hash no longer changes for that rebuild, so [contenthash].js can keep the old name despite different inline-map bytes (and the devtool mapped-asset cache can also treat the asset version as unchanged). External maps with their own [contenthash] are fine, but inline source maps need their map payload reflected somewhere in this hash.

Useful? React with 👍 / 👎.

@github-actions

github-actions Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

📦 Binary Size-limit

Comparing 2fe2afe to chore: bump patch of swc_core and swc_typescript (#14066) by CPunisher

🙈 Size remains the same at 61.99MB

@codspeed

codspeed Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 34 untouched benchmarks
⏩ 25 skipped benchmarks1


Comparing fix/hmr-codegen-result-hash-skip-source-map (2fe2afe) with main (2cc9932)

Open in CodSpeed

Footnotes

  1. 25 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@github-actions

github-actions Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Rsdoctor Bundle Diff Analysis

Found 6 projects in monorepo, 0 projects with changes.

📊 Quick Summary
Project Total Size Change
popular-libs 1.7 MB 0
react-10k 5.7 MB 0
react-1k 826.3 KB 0
react-5k 2.7 MB 0
rome 1.6 MB 0
ui-components 4.8 MB 0

Generated by Rsdoctor GitHub Action

Code generation hashes shift with the previous commit because we now
ignore source-map metadata in the digest. Update stats snapshots to
reflect the new hashes; no behavioral change in the test expectations.
@stormslowly
stormslowly marked this pull request as ready for review May 18, 2026 02:58
@stormslowly
stormslowly requested a review from hardfist May 18, 2026 02:58
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented May 18, 2026 •

Copy link
Copy Markdown

Deploying rspack with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3651d9f
Status: ✅  Deploy successful!
Preview URL: https://3e4f54d0.rspack-v2.pages.dev
Branch Preview URL: https://fix-hmr-codegen-result-hash.rspack-v2.pages.dev

View logs

Follow-up on the previous commit: keep `Source::hash` (which routes
through `CachedSource`'s memoised u64) for non-SourceMapSource entries,
and switch to `source.buffer()` only when the contained source is a
`SourceMapSource`. That preserves the upstream codegen hashes for every
source type that already hashed only the emitted bytes (RawStringSource,
OriginalSource, etc.), and removes the CodSpeed regressions in
`persistent_cache_restore` and `create_chunk_ids` that the prior
unconditional-buffer hash caused.

Also restore `chunk_init_fragments` to the digest; it was removed in the
previous commit while exploring causes, but is unrelated to the SFC HMR
bug and was already in the digest on main.

Add unit tests covering the three relevant cases (RawStringSource,
SourceMapSource, CachedSource<SourceMapSource>) so future refactors of
the helper cannot silently regress the Vue 2 multi-block SFC behavior.

Net diff vs main: the helper itself and its tests. Stats snapshots
revert to their upstream values because non-SourceMapSource entries
keep the same hash output as before.
@stormslowly

Copy link
Copy Markdown
Contributor Author

Closing — the framing of this PR ("align with webpack's hash inputs") is factually wrong. webpack-sources SourceMapSource.updateHash also hashes the source map buffer, so the divergence I cited doesn't exist. Re-investigating the real reason why webpack doesn't include byte-identical sub-modules in hot-update chunks before re-filing.

@chenjiahan
chenjiahan deleted the fix/hmr-codegen-result-hash-skip-source-map branch October 22, 2026 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Child HMR not working when parent component has HMR

2 participants